package com.tl.khadmin.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tl.khadmin.bean.News;
import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.service.ProductIService;
import com.tl.khadmin.service.RegistIService;
import com.tl.khadmin.util.Constant;

/**
 *  供应商入驻/注册
 * @author 123 
 */
@RestController
@RequestMapping(value = "/regist")
public class RegistController {

	@Resource
	private RegistIService registService;
	
	@RequestMapping(value="/img")  
	@ResponseBody
	public Map<String, String> upLoadImg(HttpSession session, 
			@RequestParam("file") MultipartFile file){
		String path = session.getServletContext().getRealPath("/upload/productimg");
		return registService.upLoadImg(path,file); 
	}
	
	@RequestMapping(value = "/do")
	@ResponseBody
	public String regist(Supplier supplier){
		return registService.regist(supplier);
	}
	
//	@RequestMapping(value = "/do")
//	@ResponseBody
//	public void regist(HttpSession session,HttpServletRequest request,HttpServletResponse response) {
//        DiskFileItemFactory factory = new DiskFileItemFactory();    
//        ServletFileUpload upload = new ServletFileUpload(factory);  
//        try {  
//            List items = upload.parseRequest(request);  
//            Iterator it = items.iterator();  
//            Supplier supplier = new Supplier();
//            while (it.hasNext()) {  
//                FileItem item = (FileItem) it.next();  
//                if (item.isFormField()) { // 如果是表单域  
//                    if (item.getFieldName().equals("supplierName")) {  
//                        String supplierName = item.getString("UTF-8");  
//                        supplier.setSupplierName(supplierName);
//                    }  
//                    if (item.getFieldName().equals("contactName")) {  
//                        String contactName = item.getString("UTF-8");  
//                        supplier.setContactName(contactName);
//                    }
//                    if (item.getFieldName().equals("contactPhone")) {  
//                        String contactPhone = item.getString("UTF-8");  
//                        supplier.setContactPhone(contactPhone);
//                    }  
//                    if (item.getFieldName().equals("remark")) {  
//                        String remark = item.getString("UTF-8");  
//                        supplier.setRemark(remark);
//                    }    
//                } else { // 如果是文件  
//                    if (item.getName() != null && !item.getName().equals("")) {  
////                    	File file = new File(root);
////                    	if(!file.exists()) {
////                    		file.mkdirs();          
////                    	} 
////                        file = new File(root,item.getName());  
////                        supplier.setTitleimg("/news/titleImg/"+item.getName());  
////                        item.write(file);  
//                    }  
//                }  
//            }  
//            supplier.setCreateTime(new Date()); 
//    		int result = registService.regist(supplier); 
//    		if(result == 1) {
//    			response.setContentType("text/html; charset=UTF-8"); //转码
//        	    PrintWriter out = response.getWriter();
//        	    out.flush();
//        	    out.println("<script>");
//        	    out.println("alert('入驻成功，资料审核中');");
//        	    out.println("history.back();");
//        	    out.println("</script>");
//    		} else { 
//    			response.setContentType("text/html; charset=UTF-8"); //转码
//        	    PrintWriter out = response.getWriter();
//        	    out.flush();
//        	    out.println("<script>");
//        	    out.println("alert('提交失败');");
//        	    out.println("history.back();");
//        	    out.println("</script>");
//    		}
//        } catch (Exception e) {   
//        	e.printStackTrace();  
//        	response.setContentType("text/html; charset=UTF-8"); //转码
//     	    PrintWriter out;
//			try {
//				response.setContentType("text/html; charset=UTF-8"); //转码
//        	    out = response.getWriter();
//        	    out.flush();
//        	    out.println("<script>");
//        	    out.println("alert('提交失败');");
//        	    out.println("history.back();");
//        	    out.println("</script>");
//			} catch (IOException e1) {
//				e1.printStackTrace();
//			}
//        }
//	}
}
